Report post

Should I use body onload>?

There's not much reason to use ; anyway when you can use window.onload. In Opera, the event target for window.onload and (and even window.addEventListener ("load", func, false)) will be the window instead of the document like in Safari and Firefox. But, 'this' will be the window across browsers.

What is the difference between body onload and window onload?

With , document.body.onload might be null, undefined or a function depending on the browser (although getAttribute ("onload") should be somewhat consistent for getting the body of the anonymous function as a string). With window.onload, when you assign a function to it, window.onload will be a function consistently across browsers.

How does the onload attribute work?

The onload attribute works the same way, except that it takes a function as its value instead of a string like the value attribute does. That also explains why you can "only use one of them" - calling window.onload reassigns the value of the onload attribute for the body tag.

How do I use a window onload event in JavaScript?

window.onload = myOnloadFunc and are different ways of using the same event. Using window.onload is less obtrusive though - it takes your JavaScript out of the HTML.

The World's Leading Crypto Trading Platform

Get my welcome gifts